package com.education.user.service.impl;


import com.education.common.utils.QiniuUtil;
import com.education.user.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


@Service
public class FileServiceImpl implements FileService {

    private static final Logger logger = Logger.getLogger(FileServiceImpl.class);

    @Override
    public Map<String, List<String>> uploadImgs2(MultipartFile file) {
        Map<String, List<String>> resultMap = new HashMap<>();
        List<String> list = new LinkedList<>();
        String result = null;

//        for (int i = 0; i < file.length; i++) {
        String fileName = file.getOriginalFilename();

        // 创建一个临时目录文件
        String tempFiles = "temp/" + fileName;
        File dest = new File(tempFiles);
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }

        BufferedOutputStream out = null;
        QiniuUtil qn = new QiniuUtil();

        try {
            out = new BufferedOutputStream(new FileOutputStream(dest));
            out.write(file.getBytes());
            result = qn.uoloapQiniu(dest, fileName);

            if (StringUtils.isNotBlank(result)) {
                list.add(result);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e1) {
            e1.getMessage();
        } finally {
            try {
                if (null != out) {
                    out.flush();
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (dest.getParentFile().exists()) {
                dest.delete();
            }
        }
//        }
        logger.info("imagesList == " + list);
        if (list.isEmpty()) {
            list.add("error");
        }
        resultMap.put("result", list);
        return resultMap;
    }

    /**
     * 七牛云上传图片
     * @param file
     * @return
     */

/*    @Override
    public Map<String, List<String>> uploadImgs(MultipartFile[] file) {

        Map<String, List<String>> resultMap = new HashMap<>();
        List<String> list = new LinkedList<>();
        String result = null;
        //遍历文件列表
        for (int i = 0; i < file.length; i++) {
            String fileName = file[i].getOriginalFilename();

            //// 创建一个临时目录文件
            //String tempFiles = "/temp" + fileName;
            //File dest = new File(tempFiles);
            //if (!dest.getParentFile().exists()) {
            //    dest.getParentFile().mkdirs();
            //}
            //
            //BufferedOutputStream out = null;
            QiniuUtil qn = new QiniuUtil();

            try {
                //out = new BufferedOutputStream(new FileOutputStream(dest));
                //out.write(file[i].getBytes());
                result = qn.upload2QiniuAndString(file[i].getBytes(),fileName);

                if (StringUtils.isNotBlank(result)) {
                    list.add(result);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e1) {
                e1.getMessage();
            }
            //finally{
            //try {
            //    //if (null != out) {
            //    //    out.flush();
            //    //    out.close();
            //    //}
            //} catch (IOException e) {
            //    e.printStackTrace();
            //}
            //
            //if (dest.getParentFile().exists()) {
            //    dest.delete();
            //}
            //}
        }

        logger.info("imagesList == " + list);
        if (list.isEmpty()) {
            list.add("error");
        }
        resultMap.put("result",list);
        return resultMap;
    }*/

    /**
     * 七牛云上传图片
     *
     * @param file
     * @return
     */
    @Override
    public Map<String, List<String>> uploadImgs(MultipartFile[] file) {

        Map<String, List<String>> resultMap = new HashMap<>();
        List<String> list = new LinkedList<>();
        String result = null;
        //遍历文件列表
        for (int i = 0; i < file.length; i++) {
            String fileName = file[i].getOriginalFilename();

            QiniuUtil qn = new QiniuUtil();
            try {
                result = qn.upload2QiniuAndString(file[i].getBytes(), fileName);

                if (StringUtils.isNotBlank(result)) {
                    list.add(result);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e1) {
                e1.getMessage();
            }
        }

        logger.info("imagesList == " + list);
        if (list.isEmpty()) {
            list.add("error");
        }
        resultMap.put("result", list);
        return resultMap;
    }

    @Override
    public void deleteQiniuImage(String fileName) {
        QiniuUtil.deleteFileFromQiniu(fileName);
    }
}